<?php

/*===============微信商城=================*/
/*----------------------------------------*/
/* $Author: 疯中追风                      */
/*----------------------------------------*/
/* $Time: 2017/10/16 16:06                */
/*----------------------------------------*/
/*=============用户表相关表===============*/

namespace   Home\Controller;
use  Think\Controller;

class UserController  extends  Controller{
	//新建收货地址
	public function add_address(){
		//查询地址,并获取省份信息
		$province = M('Region_1') -> where(array("parent_id" => 0,"level" => 1)) -> select();
		$this -> assign('province',$province);
		$this -> display();
	}	

	//将收货地址保存到数据库
	public function get_address(){
		//获取收货地址数据
		$post = I('post.');
		//获取id
		$id = cookie('id');
		$post['user_id'] = $id;
		//将所有的默认收货地址改为false
		$count = M('User_address') -> select();
		$model = M('User_address');
		for($i = 0;$i < count($count);$i ++){
			$model -> is_default = 0;
			M('User_address') -> where("address_id =".$count[$i]['address_id']. " and user_id = $id") -> save();
		}
		$post['is_default'] = 1;
		$result = M('User_address') -> add($post);
		if($result){
			//重定向地址
			redirect(U("Home/Order/order",array("id"=>$id)));
		}
	}

	//收货地址列表
	public function showAddressList(){
		//获取id
		$getId = cookie('id');
		$address_arr = M('User_address')  -> where("user_id = $getId") -> select();
		//获取省级,市级,区级,镇级以及详情地址
		$regin_model = M('Region_1');
		//将id查询出城市名字并替换。
		for($i=0;$i<count($address_arr);$i++){
				$address_arr[$i]['province'] = $regin_model -> where("id = ".$address_arr[$i]['province']) -> find()['name'];
				$address_arr[$i]['city']     = $regin_model -> where("id = ".$address_arr[$i]['city']) -> find()['name'];
				$address_arr[$i]['district']  = $regin_model -> where("id = ".$address_arr[$i]['district']) -> find()['name'];
				$address_arr[$i]['twon']   	= $regin_model -> where("id = ".$address_arr[$i]['twon']) -> find()['name'];
		}
		$this -> assign("address_arr",$address_arr);
		$this -> display();
	}


	//删除收货地址
	public function del_address(){
		$result = M('User_address') -> where("address_id =".I('get.id')) -> delete();
	}

	//获取一级城市
	public function getRegion(){
        $parent_id = I('get.parent_id');
        $selected = I('get.selected',0);        
        $data = M('Region_1')->where("parent_id=$parent_id")->select();
        $html = '';
        if($data){
            foreach($data as $h){
            	if($h['id'] == $selected){
            		$html .= "<option value='{$h['id']}' selected>{$h['name']}</option>";
            	}
                $html .= "<option value='{$h['id']}'>{$h['name']}</option>";
            }
        }
        echo $html;
    }
    
    //获取二级城市
    public function getTwon(){
    	$parent_id = I('get.parent_id');
    	$data = M('Region_1')->where("parent_id=$parent_id")->select();
    	$html = '';
    	if($data){
    		foreach($data as $h){
    			$html .= "<option value='{$h['id']}'>{$h['name']}</option>";
    		}
    	}
    	if(empty($html)){
    		echo '0';
    	}else{
    		echo $html;
    	}
    }


}

?>